<template>
  <BackageContainer>
    <jc-bar
      :customStyle="{ backgroundColor: 'transparent' }"
      :prve-btn="true"
      :search-btn="false"
      @prve="$router.replace({ path: '/home' })"
      :extension-btn="true"
      color="none"
    >
      <template> <v-spacer></v-spacer></template>
    </jc-bar>
    <v-main> </v-main>
  </BackageContainer>
</template>
<script>
import BackageContainer from "../../layout/BackageContainer.vue";
import JcBar from "../../layout/JcBar";
import WKSDK, { ConnectStatus, Channel, ChannelTypePerson, MessageText, Setting } from "wukongimjssdk";
import IMConfig from "./imconfig";
export default {
  name: "StarIm",
  components: {
    BackageContainer,
    JcBar,
  },
  created() {
    // 单机模式可以直接设置地址
    WKSDK.shared().config.addr = IMConfig.addr; // 默认端口为5200
    // 认证信息
    WKSDK.shared().config.uid = "18801263882"; // 用户uid（需要在悟空通讯端注册过）
    WKSDK.shared().config.token = "abcdefg"; // 用户token （需要在悟空通讯端注册过）
    // 连接
    WKSDK.shared().connectManager.connect();
    // 连接状态监听
    WKSDK.shared().connectManager.addConnectStatusListener((status, reasonCode) => {
      if (status === ConnectStatus.Connected) {
        console.log("连接成功");
        /**
         *  发送消息
         * @param content  消息内容
         * @param channel 频道对象 个人频道，群频道
         * @returns 完整消息对象
         */
        const setting = Setting.fromUint8(0);
        const content = new MessageText("我来也");
        const channel = new Channel("18801263881", ChannelTypePerson, setting);
        WKSDK.shared().chatManager.send(content, channel);
      } else {
        console.log("连接失败", reasonCode); //  reasonCode: 2表示认证失败（uid或token错误）
      }
    });

    // 消息发送状态监听
    WKSDK.shared().chatManager.addMessageStatusListener(this.listen);
    WKSDK.shared().chatManager.addMessageListener(this.messageformListen);
    WKSDK.shared().chatManager.addCMDListener(this.cmdListener);
  },
  destroyed() {
    // 消息发送状态监听
    WKSDK.shared().chatManager.removeMessageStatusListener(this.listen);
    WKSDK.shared().chatManager.removeMessageListener(this.messageformListen);
    WKSDK.shared().chatManager.removeCMDListener(this.cmdListener);
    // 断开
    WKSDK.shared().connectManager.disconnect();
  },
  methods: {
    listen(packet) {
      console.log("消息clientSeq->", packet); // 消息客户端序号用来匹配对应的发送的消息
      if (packet.reasonCode === 1) {
        // 发送成功
        console.log("发送成功");
      } else {
        // 发送失败
        console.log("发送失败");
      }
    },

    messageformListen(message) {
      // message.content // 消息内容
      // message.channel // 消息频道
      // message.fromUID // 消息发送者
      console.log("收到消息了-->", message);
    },

    cmdListener(message) {
      console.log("收到cmd命令消息了-->", message);
      const cmdContent = message.content;
      const cmd = cmdContent.cmd; // 指令名称
      const param = cmdContent.param; // 指令参数
    },
  },
};
</script>
